#include "stack.h"


LTNode* BuyNode(STDataType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror(" BuyNode()");
	}

	newnode->val = x;
	newnode->next = NULL;
	return newnode;
}
void STInit(ST* ps)
{
	assert(ps);
	ps->top = NULL;
	ps->size = 0;
}
void STDestroy(ST* ps)
{
	assert(ps);
	LTNode* cur = ps->top;
	while (cur)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->top = NULL;
	ps->size = 0;
}
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	LTNode* newnode = BuyNode(x);
	if (ps->top == NULL)
	{
		ps->top = newnode;
	}
	else
	{
		newnode->next = ps->top;
		ps->top = newnode;
	}
	ps->size++;
}

void STPop(ST* ps)
{
	assert(!STEmpty(ps));
	if (ps->top->next == NULL)
	{
		free(ps->top);
		ps->top = NULL;
	}
	else
	{
		LTNode* next = ps->top->next;
		free(ps->top);
		ps->top = next;
	}
	ps->size--;
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == NULL;
}

STDataType STTop(ST* ps)
{
	assert(!STEmpty(ps));
	return ps->top->val;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->size;
}



























